package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.service.ScoreService;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lk
 * @date 2020/1/12 0012 11:32
 * @description: 积分中心（包含魔法市场与积分兑换中心）
 */
@RestController
public class ScoreController {
    @Autowired
    ScoreService scoreService;
    @Autowired
    LoginService loginService;


    /**
     * 获取积分兑换中心概述
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/scorecenter", method = RequestMethod.GET)
    public String getScoreSummery(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = scoreService.getSummary(u_id, "score");
            return JSON.toJSONString(result);
        }

    }

    /**
     * 获取魔法市场概述
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/scorecenter/magiccenter", method = RequestMethod.GET)
    public String getScoreMagicSummery(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = scoreService.getSummary(u_id, "magic");
            return JSON.toJSONString(result);
        }

    }

    /**
     * 积分兑换（积分中心+魔法市场）
     *
     * @param token
     * @param scru_id
     * @return
     */
    @RequestMapping(value = "/scorecenter/exchange", method = RequestMethod.POST)
    public String ScoreExchange(@CookieValue("token") String token,
                                @RequestParam(value = "scru_id") String scru_id) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = scoreService.scoreExch(u_id, scru_id);
            return JSON.toJSONString(result);
        }

    }

    /**
     * 兑换中心抽奖 (道具)
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/center/lottery", method = RequestMethod.GET)
    public String centrDraw(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = scoreService.centerLottery(u_id);
            return JSON.toJSONString(result);
        }
    }

    /**
     * 魔法市场抽奖
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/magic/lottery", method = RequestMethod.GET)
    public String magicDraw(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = scoreService.magicLottery(u_id);
            return JSON.toJSONString(result);
        }
    }

    /**
     * 获取魔法市场剩余交易次数
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/magic/tradetimes", method = RequestMethod.GET)
    public String getmarkettimes(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            int times = loginService.getMarketTimes(u_id);
            Result result = new Result(ResultCode.SUCCESS);
            result.setData(times);
            return JSON.toJSONString(result);
        }

    }
}
